package ejbcareader;

import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.List;
import javax.xml.namespace.QName;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.ejbca.core.protocol.ws.client.gen.Certificate;
import org.ejbca.core.protocol.ws.client.gen.EjbcaWS;
import org.ejbca.core.protocol.ws.client.gen.EjbcaWSService;
import org.ejbca.core.protocol.ws.client.gen.UserDataVOWS;
import org.ejbca.core.protocol.ws.client.gen.UserMatch;
import org.ejbca.core.protocol.ws.common.CertificateHelper;

import ejbcareader.reader.CertificationAuthorityReaderFactory;
import ejbcareader.reader.EjbcaWsReader;
import ejbcareader.reader.ICertificationAuthorityReader;
import ejbcareader.reader.pojo.CertificatePojo;
import ejbcareader.utils.FileHelper;
import ejbcareader.utils.PropertiesEnum;
import ejbcareader.utils.PropertyHelper;
 

public class Init {

	private static Logger log = Logger.getLogger(Init.class);
	
	public static void main(String[] args) {
		
		DOMConfigurator.configureAndWatch("log4j.xml");
		
		log.info("Process START!");
		
		log.info("Getting instance of the reader.");
		
		EjbcaWsReader reader = (EjbcaWsReader)CertificationAuthorityReaderFactory.GetInstance();
		
		if (reader==null)
		{
			log.fatal("Unable to get instance of the reader.");
			return;
		}

		try
		{
			log.debug("Invoking the reader.");
			List<CertificatePojo> certificates = reader.Read();
			
			log.debug("Dumping reader results to output file.");
			FileHelper.Write(certificates, PropertyHelper.GetValue(PropertiesEnum.OutputFileDestinationFolder));				
		}
		catch(Exception ex)
		{
			log.error("Error reading from CA.", ex);
		}
		
		log.info("Process END!");
	}
}

/*
package ejbcareader;

import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.List;
import javax.xml.namespace.QName;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.ejbca.core.protocol.ws.client.gen.Certificate;
import org.ejbca.core.protocol.ws.client.gen.EjbcaWS;
import org.ejbca.core.protocol.ws.client.gen.EjbcaWSService;
import org.ejbca.core.protocol.ws.client.gen.UserDataVOWS;
import org.ejbca.core.protocol.ws.client.gen.UserMatch;
import org.ejbca.core.protocol.ws.common.CertificateHelper;

import ejbcareader.utils.PropertiesEnum;
import ejbcareader.utils.PropertyHelper;
 

public class Init {

	private static Logger log = Logger.getLogger(Init.class);
	
	private static EjbcaWS ejbcaraws;

	public static void main(String[] args) {
		
		DOMConfigurator.configureAndWatch("log4j.xml");
			
		log.info("Starting EJBCAReader process...");
        String urlstr = PropertyHelper.GetValue(PropertiesEnum.EjbcaWSServiceWsdl);        
        log.info(String.format("Contacting webservice at [%s]", urlstr));
        		        
        System.setProperty("javax.net.ssl.trustStore",PropertyHelper.GetValue(PropertiesEnum.TrustStore));
        System.setProperty("javax.net.ssl.trustStorePassword",PropertyHelper.GetValue(PropertiesEnum.TrustStorePassword));

        System.setProperty("javax.net.ssl.keyStore",PropertyHelper.GetValue(PropertiesEnum.KeyStore));
        System.setProperty("javax.net.ssl.keyStorePassword",PropertyHelper.GetValue(PropertiesEnum.KeyStorePassword));        
        
        QName qname = new QName("http://ws.protocol.core.ejbca.org/", "EjbcaWSService");
        
        EjbcaWSService service = null;
        try {
			service = new EjbcaWSService(new URL(urlstr),qname);
		} catch (MalformedURLException ex) {
			log.error("Error creating URL to invoke.", ex);
		}        	
        
		if (service!=null) 
		{
			ejbcaraws = service.getEjbcaWSPort();
			
			log.info(String.format("GetEJBCAVersion [%s]",ejbcaraws.getEjbcaVersion()));
						
			// Search pattern
			UserMatch usermatch = new UserMatch();
			usermatch.setMatchwith(UserMatch.MATCH_WITH_USERNAME);
			usermatch.setMatchtype(UserMatch.MATCH_TYPE_CONTAINS);
			usermatch.setMatchvalue("CYPR");
			
			try {
														
				List<UserDataVOWS> caUsers = ejbcaraws.findUser(usermatch);
				
				for(UserDataVOWS caUser: caUsers){
					log.debug(String.format("Found User: [%s]",
									caUser.getUsername() + ", \""
                                    + caUser.getSubjectDN() + "\", \""
                                    + caUser.getSubjectAltName() + "\", "
                                    + caUser.getEmail() + ", "
                                    + caUser.getStatus() + ", "
                                    + caUser.getType() + ", "
                                    + caUser.getTokenType() + ", "
                                    + caUser.getHardTokenIssuerName()));
					
					List<Certificate> userCertificates = ejbcaraws.findCerts(caUser.getUsername(), true);
					for(Certificate cert : userCertificates){
												
						X509Certificate x509cert = (X509Certificate)  
								CertificateHelper.getCertificate(cert.getCertificateData());					
							

						log.debug(x509cert.getNotAfter());
						log.debug(String.format("Found Certificate: [%s]", cert.getRawCertificateData()));
					}
				}
							
				log.info("EJBCAReader process end...");
				
			} catch (Exception ex){	
				log.error("Exception caught...", ex);
			}  
		}   
	}
}
*/